package com.yuxi.controller.manager;

import com.yuxi.common.NumberUtil;
import com.yuxi.common.SkJsonResult;
import com.yuxi.common.StringUtil;
import com.yuxi.pubInter.BaseSearch;
import com.yuxi.model.Rights;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;
import com.yuxi.pubInter.exception.SkException;
import com.yuxi.pubInter.IBaseService;
import com.yuxi.pubInter.RestfulController;
import com.yuxi.dto.RightsDTO;
import com.yuxi.dto.RightsPageDTO;
import com.yuxi.modelExt.RightsExt;
import com.yuxi.searchExt.RightsSearchExt;
import com.yuxi.service.IRightsService;
import com.yuxi.vo.RightsDetailVO;
import com.yuxi.vo.RightsListVO;
import com.yuxi.vo.RightsDomainVO;
import com.yuxi.vo.RightsDetailDomainVO;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Set;
import java.util.HashSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
*    @author sunke
*    @Date 2023-10-13 10:28:19
*    @description RightsController      权限表
*/
@Api(tags = "后台管理系统-权限表相关", description = "权限表相关接口")
@RestController(value ="rightsRestfulController")
@RequestMapping("/manager/rights/")
@Validated
public class RightsController extends ApiManageController<RightsDTO,RightsPageDTO,RightsExt, RightsSearchExt,RightsDomainVO,RightsDetailDomainVO,RightsDetailVO,RightsListVO,String> {

	@Resource(name = "rightsService")
	private IRightsService rightsService;



    @ApiOperation(value = "权限下级树", notes = "权限下级树", httpMethod = "POST")
    @PostMapping({"tree/{level}"})
    public SkJsonResult<List<RightsDetailDomainVO>> tree(@PathVariable int level, HttpServletRequest request, HttpSession session) throws SkException {

        return SkJsonResult.ok().setData(rightsService.tree(level));

    }


    @ApiOperation(value = "权限下级树", notes = "权限下级树", httpMethod = "POST")
    @PostMapping({"detailTree/{id}"})
    public SkJsonResult<RightsDetailVO> detailTree(@PathVariable String id, HttpServletRequest request, HttpSession session) throws SkException {

        RightsExt rights = rightsService.treeByRightsId(id, -1);

        return SkJsonResult.ok().setData(mapDetail(rights, RightsDetailVO.class));
    }


    @ApiOperation(value = "顶级列表", notes = "顶级列表")
    @PostMapping(value = "topList")
    public SkJsonResult<RightsListVO> topList(@RequestBody RightsPageDTO pageDTO, HttpServletRequest request, HttpSession session) throws SkException {


        pageDTO.setLevelGreater(1);
        pageDTO.setLevelLess(1);
        pageDTO.setOrderBy(Rights.SORT_NUM);
        pageDTO.setOrderDesc(BaseSearch.ASC_ORDER);
        return super.list(pageDTO, request, session);
    }


    @ApiOperation(value = "第二级列表", notes = "第二级列表")
    @PostMapping(value = "secondList")
    public SkJsonResult<RightsListVO> secondList(@RequestBody RightsPageDTO pageDTO, HttpServletRequest request, HttpSession session) throws SkException {
        pageDTO.setLevelGreater(2);
        pageDTO.setLevelLess(2);
        pageDTO.setOrderBy(Rights.SORT_NUM);
        pageDTO.setOrderDesc(BaseSearch.ASC_ORDER);
        return super.list(pageDTO, request, session);
    }


    @Override
    public boolean beforePostAdd(RightsDTO dto, RightsExt domain, HttpServletRequest request, HttpSession session) throws SkException {
        if (StringUtil.isNullOrEmpty(dto.getUpId())) {
            domain.setLevel(1);
        } else {
            RightsExt upRights = rightsService.getDetail(dto.getUpId());
            domain.setLevel(NumberUtil.parseInt(upRights.getLevel()) + 1);
        }

        return super.beforePostAdd(dto, domain, request, session);
    }


    @Override
    public void afterPostMod(RightsDTO dto, RightsExt domain, SkJsonResult<RightsDetailVO> jsonResult, HttpServletRequest request, HttpSession session) throws SkException {
        super.afterPostMod(dto, domain, jsonResult, request, session);

        //说明上级没变，等级就没变
        if (StringUtil.toString(domain.getUpId()).equalsIgnoreCase(StringUtil.toString(dto.getUpId()))) {
            return;
        }
        int level = 1;
        if ("".equalsIgnoreCase(domain.getUpId())) {
            level = 1;
        } else {
            RightsExt upRights = rightsService.getDetail(domain.getUpId());

            level = NumberUtil.parseInt(upRights.getLevel(), 1) + 1;


            rightsService.updateAllSubLevelByRightsId(domain.getId(), level, true);


        }


    }


	@Override
	public IBaseService<RightsExt, RightsSearchExt,String> getBaseService() throws SkException {
		return rightsService;
	}

	@Override
	public String getBaseRoute() {
		return "/manager/rights/";
	}

	/**
	* 当前模板名称
	* @return
	*/
	@Override
	public String getModelName(){
		return "权限表";
	}



	@Override
	public Set<String> totalMapSet() {
		Set totalMapSet=new HashSet();
		totalMapSet.add("count(rights.r_id) as countNum");
		return totalMapSet;
	}


}